package com._public._ui 
{
	import com._public._net.LoadData;
	import com._public._utils.MathUtil;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.geom.Point;

	/**
	 * ...
	 * @author WZH(shch8.com)
	 */
	public class LoadBox extends Sprite
	{
		private var loader:Loader
		private var _loadTip:DisplayObject
		private var errSpr:DisplayObject
		private var _errTip:Object
		private var maskSpr:Shape
		private var _defaultUrl:String
		private var isLoadDefault:Boolean;
		/**
		 * 用于存放图片索引中特定的ID
		 */
		public var id:String=""
		/**
		 * 是否布满显示
		 */
		public var fullShow:Boolean = false;
		public function LoadBox(url:String="",loadTip:DisplayObject=null,errTip:Object=null) 
		{
			maskSpr = new Shape
			maskSpr.graphics.beginFill(0x999999,0.4)
			maskSpr.graphics.drawRect(0, 0, 100, 100)
			maskSpr.graphics.endFill();
			addChild(maskSpr)
			this.url = url;
			_loadTip = loadTip
			_errTip=errTip
		}
		/**
		 * 缺省图片，加载失败时的取图路径，请在图片加载完成之前设置有效
		 */
		public function set defaultUrl(value:String):void
		{
			_defaultUrl=value;
		}
		/**
		 * 设置加载对象的URL
		 */
		public function set url(value:String):void
		{
			if(value=="")return
			if (errSpr != null) {
				if (errSpr.stage != null) removeChild(errSpr);
				}
			if (loader != null) {
				if (loader.stage != null) removeChild(loader);
				}
			loader = LoadData.getInstance().loadFile(value, loadComplete, loadErr)
		}
		/**
		 * 清除加载对象
		 */
		public function clear():void
		{
			loader.unloadAndStop()
			loader = null;
		}
		private function loadComplete(_loader:Loader):void
		{
			if (_loadTip != null) {
				if (_loadTip.stage != null) removeChild(_loadTip);
				}
			_loadTip = _loader;
			setSize()
			addChild(_loader);
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		private function loadErr():void
		{
			
			clear()
			if(_defaultUrl!=""&&!isLoadDefault){
				this.isLoadDefault=true;
				url=this._defaultUrl;
				return;
			}
			if (_loadTip != null) {
				if(_loadTip.stage!=null)removeChild(_loadTip)
				}
			if (_errTip is String) {
				
				}else if (_errTip is DisplayObject) {
					errSpr=_errTip as DisplayObject
					addChild(errSpr)
					}
			this.dispatchEvent(new ErrorEvent(ErrorEvent.ERROR));
		}
		private function setSize():void
		{
			if (_loadTip != null) {
				_loadTip.mask = maskSpr;
				var point:Point = MathUtil.limitSize(_loadTip.width, _loadTip.height, maskSpr.width, maskSpr.height, !fullShow);
				_loadTip.width = point.x
				_loadTip.height = point.y
				addChild(_loadTip)
			}
			if (_loadTip != null) {
				_loadTip.x = (maskSpr.width - _loadTip.width) * .2
				_loadTip.y = (maskSpr.height - _loadTip.height) * .2
				}
		}
		
		override public function get width():Number 
		{
			if(loader!=null)return loader.width;
			return maskSpr.width;
		}
		
		override public function set width(value:Number):void 
		{
			maskSpr.width = value;
			setSize()
		}
		override public function get height():Number 
		{
			if(loader!=null)return loader.height;
			return maskSpr.height;
		}
		
		override public function set height(value:Number):void 
		{
			maskSpr.height = value;
			setSize()
		}
	}

}